package splab.ufcg.edu.br.testlink;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {

	public static void main(String[] args) throws SQLException {

//		if (args.length != 1) {
//			printHelp();
//		}

		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;

		try {

			String jdbcDriverStr;
			String jdbcURL = "jdbc:mysql://epol.lsd.ufcg.edu.br:3306/testlink";

			connection = DriverManager.getConnection(jdbcURL, "arthur",
					"123mudar");
			statement = connection.createStatement();

			StringBuilder query = new StringBuilder();

			query.append("select kw.keyword as req, 'Test Case' as source, tc.name as name ");
			query.append("from nodes_hierarchy as tc,  ");
			query.append("testcase_keywords as kwtc,  ");
			query.append("keywords as kw ");
			query.append("where kwtc.keyword_id = kw.id and ");
			query.append("kwtc.testcase_id = tc.id and  ");
			query.append("tc.parent_id in (  ");
			query.append("select uc.id from nodes_hierarchy as uc where uc.parent_id in ( ");
			query.append("select re.id from nodes_hierarchy as re where re.parent_id = ");
			query.append("(select proj.id from nodes_hierarchy as proj where proj.node_type_id = 1 and proj.name = 'e-Pol'))); ");

			resultSet = statement
					.executeQuery(query.toString());
			
			while (resultSet.next()) {
	            

				String requirement = resultSet.getString("req");
				String source = resultSet.getString("source");
				String artifact = resultSet.getString("name");
				
				
	            System.out.println(requirement + " " + source + " " + artifact);
	        }
			
			// "select * from testlink.testcase_keywords;"
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (statement != null) {
				statement.close();
			}

			if (resultSet != null) {
				resultSet.close();
			}

			if (connection != null) {
				connection.close();
			}
		}
	}

	private static void printHelp() {
		System.out
				.println("------------------------------------------------------------");
		System.out.println("Argumentos inválidos para execução!");

		System.out
				.println("Favor informar o caminho do arquivo: <parser-testlink.properties>");
		System.out.println("Arquivo deve conter as seguintes propriedades:");
		System.out.println();
		System.out
				.println("	url.connection = endereço para conexão com o banco do testlink");
		System.out
				.println("	db.name = nome do banco do testlink no endereço informado");
		System.out
				.println("	db.user = nome do usuário para conectar ao banco do testlink");
		System.out
				.println("	db.passwd = senha para conectar ao banco do testlink com o usuário informado");
		System.out.println();
		System.out
				.println("------------------------------------------------------------");

	}

}
